<?php
final class lang {
	private $directory;
	private $filename;
	private $data = array();
 
	public function __construct() {
	}
	
	public function set_directory($directory = '') {
		global $_config;
		$this->directory = $directory ? $directory : $_config['lang'];
	}
	
  	public function get($key) {
		self::set_directory();
		self::load_init();
		self::load(ROUTE_M.'/'.ROUTE_C);
		if (stripos($key, '/') !== false) { 
			$a = explode('/', $key); 
			$m = $a[0];
			$c = $a[1];
			if (ROUTE_M.'/'.ROUTE_C != $m.'/'.$c) self::load($m.'/'.$c);
			$key = $a[2];
		}

   		return (isset($this->data[$key]) ? $this->data[$key] : $key);
  	}
	
	public function load($filename) {
		$file = BC_LANG_PATH . $this->directory . '/' . $filename . '.php';
    	
		if (file_exists($file)) {
			$_ = array();
	  		
			include_once $file;
		
			$this->data = array_merge($this->data, $_);

			return $this->data;
		} else {
			//trigger_error('Error: Could not load language ' . $filename . '!');
			//exit();
		}
  	}
	
	public function load_init($files = array()) {
		$_ = array();
		$files = is_array($files) && count($files) ? $files : array('_common', '_msg');
		$this->data = is_array($this->data) ? $this->data : array();
		foreach ($files as $file) {
			$file = BC_LANG_PATH . $this->directory . '/'.$file.'.php';
			if (file_exists($file)) include_once $file;
			$this->data = array_merge($this->data, $_);
		}
		return $this->data;
	}
}
?>